package com.mcc.myapplication

import javax.inject.Inject

class Person { /*……*/ }

class Empty

class Student constructor(firstName: String) { /*……*/ }
// 如果主构造函数没有任何注解或者可见性修饰符，可以省略这个 constructor 关键字
// class Student(firstName: String) { /*……*/ }

class Customer public @Inject constructor(name: String) { /*……*/ }
// 如果构造函数有注解或可见性修饰符，这个 constructor 关键字是必需的

class InitOrderDemo(name: String) {
    val firstProperty = "First property: $name".also(::println)

    init {
        println("First initializer block that prints $name")
    }

    val secondProperty = "Second property: ${name.length}".also(::println)

    init {
        println("Second initializer block that prints ${name.length}")
    }
}

class Person2(val firstName: String, val lastName: String, var age: Int)

class Person3(val firstName: String, val lastName: String, var isEmployed: Boolean = true)

// 次构造函数
class Person4(val pets: MutableList<Pet> = mutableListOf())

class Pet {
    constructor(owner: Person4) {
        owner.pets.add(this) // adds this pet to the list of its owner's pets
    }
}

class Person5(val name: String) {
    val children: MutableList<Person5> = mutableListOf()

    // 如果类有一个主构造函数，每个次构造函数需要委托给主构造函数，
    // 可以直接委托或者通过别的次构造函数间接委托
    // 委托到同一个类的另一个构造函数用 this 关键字即可
    constructor(name: String, parent: Person5) : this(name) {
        parent.children.add(this)
    }
}

class Constructors {
    init {
        println("Init block")
    }

    constructor(i: Int) {
        println("Constructor i=$i")
    }

    constructor(i: Int, ss: String) : this(i) {
        println("Constructor ss=$ss")
    }

}

// 如果一个非抽象类没有声明任何（主或次）构造函数，它会有一个生成的不带参数的主构造函数。构造函数的可见性是 public。
class DoCreateMe private constructor() { /*……*/ }

// 如果你不希望你的类有一个公有构造函数，那么声明一个带有非默认可见性的空的主构造函数
class DonotCreateMe private constructor() { /*……*/ }

// 如果主构造函数的所有的参数都有默认值，编译器会生成一个额外的无参构造函数
class Customer2(val name: String = "", val age: Int = 18)

fun main() {
    val p = Person()
    val e = Empty()
    val s = Student("SnZ")
    InitOrderDemo("YourName")
    Person2("morning", "cat", 30)
    Person3("morning", "cat")
    val p4 = Person4()
    Pet(p4)
    Pet(p4)
    p4.pets.toList().joinToString().also(::println)
    val p5 = Person5("ZhangSan")
    Person5("ZhangXiao", p5)
    Constructors(33, "mcc")
    Customer2()
}



